#region The MIT License
// The MIT License
// 
// Copyright (c) 2009 octalforty studios
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#endregion
using System;

namespace octalforty.Componento.Shared.SemanticModel
{
    public class VersionSpecification
    {
        #region Public Properties
        public Version Version 
        { get; private set; }

        public VersionSpecificationOperator SpecificationOperator 
        { get; private set; }
        #endregion

        private VersionSpecification(Version version, VersionSpecificationOperator specificationOperator)
        {
            Version = version;
            SpecificationOperator = specificationOperator;
        }

        public bool Matches(Version v)
        {
            switch(SpecificationOperator)
            {
                case VersionSpecificationOperator.Equal:
                    return v == Version;
                case VersionSpecificationOperator.Greater:
                    return v > Version;
                case VersionSpecificationOperator.GreaterOrEqual:
                    return v >= Version;
                default:
                    throw new ArgumentOutOfRangeException();
            } // switch
        }

        public static VersionSpecification Equal(Version version)
        {
            return new VersionSpecification(version, VersionSpecificationOperator.Equal);
        }

        public static VersionSpecification Greater(Version version)
        {
            return new VersionSpecification(version, VersionSpecificationOperator.Greater);
        }

        public static VersionSpecification GreaterOrEqual(Version version)
        {
            return new VersionSpecification(version, VersionSpecificationOperator.GreaterOrEqual);
        }

        #region Object Members
        public bool Equals(VersionSpecification other)
        {
            if(ReferenceEquals(null, other)) return false;
            if(ReferenceEquals(this, other)) return true;
            return Equals(other.Version, Version) && Equals(other.SpecificationOperator, SpecificationOperator);
        }

        public override bool Equals(object obj)
        {
            if(ReferenceEquals(null, obj)) return false;
            if(ReferenceEquals(this, obj)) return true;
            if(obj.GetType() != typeof(VersionSpecification)) return false;
            return Equals((VersionSpecification)obj);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                return ((Version != null ? Version.GetHashCode() : 0) * 397) ^ SpecificationOperator.GetHashCode();
            }
        }

#if DEBUG
        public override string ToString()
        {
            return string.Format("{0} {1}", SpecificationOperator, Version);
        }
#endif
        #endregion
    }
}